﻿using System;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web.Script.Serialization;

namespace AmorCanino.Common
{
    public class JSonParser : IDisposable
    {
        public T Deserialize<T>(string json)
        {
            T concreteObject = Activator.CreateInstance<T>();
            var memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(json));
            var serializer = new DataContractJsonSerializer(concreteObject.GetType());

            concreteObject = (T)serializer.ReadObject(memoryStream);
            memoryStream.Close();
            memoryStream.Dispose();
            
            return concreteObject;
        }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        ~JSonParser()
        {
            Dispose();
        }

        public string Serialize(Object serializableObject)
        {
            return new JavaScriptSerializer().Serialize(serializableObject);
        }
    }
}
